package org.jeecg.modules.online.cgform.enhance.impl.http;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.online.cgform.enhance.impl.http.base.CgformEnhanceHttpInter;
import org.jeecg.modules.online.cgform.entity.OnlCgformEnhanceJava;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * http List 增强
 *
 * @author sunjianlei
 */
@Slf4j
@Component("cgformEnhanceJavaListHttpImpl")
public class CgformEnhanceHttpListImpl implements CgformEnhanceHttpInter {

    @Override
    public void execute(String tableName, List<Map<String, Object>> dataList, OnlCgformEnhanceJava enhance) {
        JSONObject params = new JSONObject();
        params.put("tableName", tableName);
        params.put("dataList", dataList);
        // 获取返回值
        Object res = this.sendPost(params, enhance);
        // 判断返回的是否是 JSONArray
        if (res instanceof JSONArray) {
            JSONArray newDataList = (JSONArray) res;
            for (int i = 0; i < dataList.size(); i++) {
                Map<String, Object> record = dataList.get(i);
                Map<String, Object> newRecord = newDataList.getJSONObject(i);
                // 将旧值替换为新值
                record.putAll(newRecord);
            }
        }
    }

}
